package com.sense.kukoo.common.media.net.model

import com.sense.kukoo.common.media.net.MediaHttpApi
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.repo.bean.BookDetailsBean
import com.sense.kukoo.common.repo.bean.BookItemBean
import com.sense.kukoo.common.repo.bean.QueryPageBean
import com.sense.kukoo.common.repo.bean.ResultBean
import com.sense.kukoo.common.repo.http.KukooHttpApi
import com.sense.kukoo.common.repo.http.KukooHttpManager
import okhttp3.RequestBody

class MediaModel: BaseMvvmModel() {

    private val TAG: String = javaClass.simpleName

    override fun onModelCleared() {}

    /*suspend fun bookDetails(id: String): ResultBean<BookDetailsBean> {
        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).bookDetails(id)
    }

    suspend fun bookQueryPage(
        book_id: Long,
        pageIndex: Int,
        pageSize: Int,
        asc: Int,
    ): ResultBean<QueryPageBean<BookItemBean>> {
        val hashMap = HashMap<String, Any>()
        val sort = HashMap<String, Any>()
        val orders = HashMap<String, Any>()
        val query = HashMap<String, Any>()
        val terms = HashMap<String, Any>()
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        hashMap.put("sort", sort)
        sort.put("order", arrayOf(orders))
        orders.put("asc", asc)//是否正序: 0. 否; 1. 是;
        orders.put("name", "chapter")
        hashMap.put("query", query)
        query.put("terms", arrayOf(terms))
        terms.put("field", "book_id")
        terms.put("value", book_id)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).bookQueryPage(mRequestBody)
    }

    suspend fun nextChapter(
        book_id: Long,
        chapter:Int,
        asc: Int,
    ): ResultBean<QueryPageBean<BookItemBean>> {
        var chapterType = ""
        var newChapter = chapter
        if (asc==1) {
            //正序
            chapterType = "ge"
            newChapter++
        }else{
            //反序
            chapterType = "le"
            newChapter--
        }
        val hashMap = HashMap<String, Any>()
        val sort = HashMap<String, Any>()
        val orders = HashMap<String, Any>()
        val query = HashMap<String, Any>()
        val terms = HashMap<String, Any>()
        val ranges = HashMap<String, Any>()
        hashMap.put("pageIndex", 1)
        hashMap.put("pageSize", 1)
        hashMap.put("sort", sort)
        sort.put("order", arrayOf(orders))
        orders.put("asc", asc)//是否正序: 0. 否; 1. 是;
        orders.put("name", "chapter")
        hashMap.put("query", query)
        query.put("terms", arrayOf(terms))
        terms.put("field", "book_id")
        terms.put("value", book_id)
        query.put("ranges", arrayOf(ranges))
        ranges.put("field", "chapter")
        ranges.put(chapterType, newChapter)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).bookQueryPage(mRequestBody)
    }

    suspend fun listen(
        bookId: Long,
        bookItemId: Long,
        total: Long,
    ): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("bookId", bookId)
        hashMap.put("bookItemId", bookItemId)
        hashMap.put("total", total)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).listen(mRequestBody)
    }

    suspend fun isSubscription(bookId : String): ResultBean<Boolean> {
        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).isSubscription(bookId)
    }

    suspend fun subscription(bookId: String): ResultBean<Boolean> {
        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).subscription(bookId)
    }

    suspend fun isLike(bookId : String, bookItemId : String): ResultBean<Boolean> {
        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).isLike(bookId, bookItemId)
    }

    suspend fun like(bookId : String, bookItemId : String): ResultBean<Boolean> {
        return KukooHttpManager.instance.createHttpClient(MediaHttpApi::class.java).like(bookId, bookItemId)
    }

    suspend fun saveListen(
        contentId: Long,
        contentUserId: Long,
        total: Long,
        isEnding:Boolean
    ): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("contentId", contentId)
        hashMap.put("contentUserId", contentUserId)
        hashMap.put("total", total)
        if (isEnding) {
            hashMap.put("isEnding", 1)
        }
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return KukooHttpManager.instance.createHttpClient(KukooHttpApi::class.java).saveListen(mRequestBody)
    }*/
}